package com.cs.controller;

import com.cs.pojo.Menu;
import com.cs.pojo.Role;
import com.cs.resultVO.ResultVO;
import com.cs.resultVO.ResultVO3;
import com.cs.resultVO.ResultVORoleIndex;
import com.cs.service.MenuService;
import com.cs.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("role")
@ResponseBody
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;
    @RequestMapping(value = "list.do")
    public ResultVORoleIndex<Role> queryByPage(String rolename, Integer pageNum, Integer pageSize){
        if(pageNum==null||pageNum<=0){
            pageNum=1;
        }
        if(pageSize==null||pageSize<=0){
            pageSize=5;
        }
        PageInfo<Role> rolePageInfo = roleService.queryAll2(rolename, pageNum, pageSize);
        System.out.println(rolePageInfo);
        return new ResultVORoleIndex<>(rolePageInfo);
    }
    @RequestMapping("edit.do")
    public ResultVO<Role> queryById(int rId){
        Role role = roleService.queryById(rId);
        List<Role> roles = roleService.queryAll();
        return new ResultVO<>(roles,role);
    }

    @RequestMapping("updaterolelist.do")
    public ResultVO<Role> update(@RequestParam(value = "rId",required = false)int rId, @RequestParam(value = "mIdList",required = false) List<String> mIdList){
        List<Integer> mids=new ArrayList<>();
        int update=0;
        if (mIdList.size()>0){
            for (String s:mIdList){
                mids.add(Integer.parseInt(s));
            }
            for (Integer i:mids){
                update = roleService.insertMids(rId, i);
            }
            if (update>0){
                return new ResultVO<>();
            }else {
                return new ResultVO<>(500,"服务器内部出错");
            }
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }


    }

    @RequestMapping("updateroleobj.do")
    public ResultVO<Role> update2(Role role){
        int update = roleService.update(role);
        int i = roleService.deleteList(role.getrId());
        if (update+i>0){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }


    @RequestMapping("deleteone.do")
    public ResultVO<Role> deleteone(@RequestParam(value = "rId",required = false) int rId){
        int i = roleService.deleteOne(rId);
        if(i==1){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }

    @RequestMapping("getroleinfo.do")
    public ResultVO<Role> getRoleInfo(){
        List<Role> roles = roleService.queryAll();
        return new ResultVO<>(roles);
    }

    @RequestMapping("add.do")
    public ResultVO<Integer> add(Role role){
        int add = roleService.add(role);
        if (add>0){
            return new ResultVO<>(role.getrId());
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }

    @RequestMapping("getinfo.do")
    public ResultVO3 getMenuList(String rId){
        int id = Integer.parseInt(rId);
        List<Menu> menus = menuService.queryStateList();
        List<Menu> noMenus=menuService.queryStateList();;
        List<Menu> yesMenus=new ArrayList<>();
        List<String> mids = menuService.queryByRId(id);
        for(int i=0;i<mids.size();i++){
            for (int j=0;j<noMenus.size();j++){
                if (Integer.parseInt(mids.get(i))==noMenus.get(j).getmId()){
                    noMenus.remove(j);
                }
            }
        }
        System.out.println(noMenus);
        for(int i=0;i<mids.size();i++){
            for (int j=0;j<menus.size();j++){
                if (Integer.parseInt(mids.get(i))==menus.get(j).getmId()){
                    yesMenus.add(menus.get(j));
                }
            }
        }
        System.out.println(yesMenus);
        Role role = roleService.queryById(id);
        return new ResultVO3(yesMenus,noMenus,role);
    }

    @RequestMapping("getmenuinfo.do")
    public ResultVO<Menu> getMenuInfo(){
        List<Menu> menus = menuService.queryAll();
        return new ResultVO<>(menus);
    }

    @RequestMapping("getrolelist.do")
    public ResultVO<Role> getRoleList(){
        List<Role> roles = roleService.queryAll();
        return new ResultVO<>(roles);
    }
}
